package com.blog.web.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blog.common.core.domain.R;
import com.blog.common.core.utils.AddressUtils;
import com.blog.common.core.utils.ip.IpUtils;
import com.blog.common.domain.entity.Article;
import com.blog.common.domain.entity.SiteConfig;
import com.blog.common.domain.entity.UniqueView;
import com.blog.common.domain.vo.BlogHomeInfoVo;
import com.blog.common.mapper.dao.ArticleMapper;
import com.blog.common.mapper.dao.CategoryMapper;
import com.blog.common.mapper.dao.TagMapper;
import com.blog.common.mapper.dao.UniqueViewMapper;
import com.blog.common.redis.service.RedisService;
import com.blog.common.domain.enums.ArticleEnums;
import com.blog.system.api.feign.RemoteConfigService;
import com.blog.web.service.BlogInfoService;
import com.blog.web.service.PageService;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Objects;

import static com.blog.common.core.constant.ServiceConst.CITY;
import static com.blog.common.core.constant.ServiceConst.PROVINCE;
import static com.blog.common.core.constant.CacheConstants.*;

/**
 * 博客信息 服务实现类
 *
 * @author Valerie
 * @date 2023/2/18
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class BlogInfoServiceImpl implements BlogInfoService {

    private final ArticleMapper articleMapper;

    private final CategoryMapper categoryMapper;

    private final TagMapper tagMapper;

    private final PageService pageService;

    private final RedisService redisService;

    private final RemoteConfigService remoteConfigService;

    private final HttpServletRequest request;

    private final UniqueViewMapper uniqueViewMapper;



    @Override
    public BlogHomeInfoVo getBlogInfo(){
        Long articleCount = articleMapper.selectCount(
                new LambdaQueryWrapper<Article>()
                        .eq(Article::getStatus, ArticleEnums.PUBLIC.getStatus())
        );

        Long categoryCount = categoryMapper.selectCount(null);

        Long tagCount = tagMapper.selectCount(null);
        R<SiteConfig> r = remoteConfigService.getWebsiteConfig();
        SiteConfig config = r.getData();
        return BlogHomeInfoVo.builder()
                .articleCount(articleCount)
                .categoryCount(categoryCount)
                .tagCount(tagCount)
                .viewsCount(String.valueOf(0L))
                .siteConfig(config)
                .pageList(pageService.listPages())
                .build();
    }


    @Override
    public String getAbout(){
        Object value = redisService.get(ABOUT);
        return Objects.nonNull(value)? value.toString() : "";
    }


    @Override
    public void getReport() {
        final UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        String userAgentInfo = String.join(" ",userAgent.getBrowser().getName(), userAgent.getBrowserVersion().getMajorVersion(), userAgent.getOperatingSystem().getName());
        String ipAddr = IpUtils.getIpAddr(request);
        String address = AddressUtils.getRealAddressByIp(ipAddr);
        UniqueView uniqueView = UniqueView.builder()
                .id(IdUtil.getSnowflakeNextId())
                .ip(ipAddr)
                .address(address)
                .userAgent(userAgentInfo)
                .viewsCount(0L)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        // todo redis
        uniqueViewMapper.insertOrUpdateView(uniqueView);
    }
}
